package clientGUI.userPanels;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;

import clientGUI.CollegeClientGUI;
import clientGUI.defines;
import clientGUI.userPanels.Controllers.ISEGateController;
import clientGUI.userPanels.Controllers.ISEGateController.isRoles;
import clientserver.Client;

import javax.swing.JComboBox;



import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextPane;

import college.Request;
import college.RequestTreatment;
import college.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class ISEGatePanel extends UPanel {
	private JComboBox roleCB;
	private JComboBox ridCB;
	private ISEGateController controller;
	private JTextPane infoPane;
	private int stage;
	JButton btnSuspend;


	
	
	public  void SetFrame( CollegeClientGUI fr)
	{
		myframe=fr;
	User t=myframe.getUserlogged();
	controller.setuser(t);
	}
	
	public ISEGatePanel(Client c) {
		super();
		client=c;
		controller= new ISEGateController(client);
		setLayout(null);
		
		JLabel lblRelatedRequests = new JLabel("RELATED REQUESTS");
		lblRelatedRequests.setBounds(119, 0, 198, 38);
		add(lblRelatedRequests);
		
		JLabel lblRole = new JLabel("Role:");
		lblRole.setBounds(76, 76, 25, 14);
		add(lblRole);
		
		roleCB = new JComboBox(ISEGateController.isRoles.values());
		roleCB.setBounds(182, 73, 268, 20);
		
		roleCB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String selectedrole= roleCB.getSelectedItem().toString();
				controller.setSelectedRole(ISEGateController.isRoles.valueOf(selectedrole));
				
				if (selectedrole.compareTo("Supervisor") == 0){
					btnSuspend.setVisible(true);
				}else
					btnSuspend.setVisible(false);
				
				controller.setRids();				
				
					ArrayList<String> relevantrids = controller.getRids();
				
					 ridCB.removeAllItems();
					 
					    Iterator<String> itr = relevantrids.iterator();
					    String str;
					    while (itr.hasNext()){
					    	
					    	ridCB.addItem( str =itr.next());
									
					
				//	validate();
				}
			
			}
		});
		add(roleCB);
		
		JLabel lblRequestId = new JLabel("Request ID:");
		lblRequestId.setBounds(119, 101, 58, 14);
		add(lblRequestId);
		
		ridCB = new JComboBox();
		ridCB.setBounds(182, 98, 268, 20);
		ridCB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				infoPane.setText("");
				String sel=(String)ridCB.getSelectedItem();
				try {
				if(!sel.equals("None"))
					{
					Request r=new Request(client);	
					int id=Integer.parseInt((String) ridCB.getSelectedItem());
					
				r.loadFromDb(id);
				infoPane.setText(r.toString());
					
					}
				}catch (Exception ex) {
					
				}
			}
		});
		add(ridCB);
		

		infoPane = new JTextPane();
		infoPane.setBounds(129, 123, 407, 331);
		infoPane.setEditable(false);
		add(infoPane);
		
		JButton btnSelect = new JButton("Select");
		btnSelect.setBounds(450, 465, 118, 23);
		btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isRoles ir = controller.getRole();
				int rid=Integer.parseInt(ridCB.getSelectedItem().toString());  //request id choosed in Combo Box
				stage=RequestTreatment.getRequestStage(client, rid);
				System.out.println("stage="+stage);
						switch (ir) {
				case Chairman:
					CommiteePanel cmtp = new CommiteePanel(rid,client);
					myframe.SetPanel(cmtp);break;
				case ProcessLeader:
					if (stage == 9){
						ProcessLeaderDeadlinePanel pldp = new ProcessLeaderDeadlinePanel(client,rid);
						myframe.SetPanel(pldp);
					}else {
					ProcessLeaderPanel plp = new ProcessLeaderPanel(Integer.parseInt(ridCB.getSelectedItem().toString()),client,myframe.getUserlogged());
					plp.SetFrame(myframe);
					myframe.SetPanel(plp);
					}
					break;
				case Supervisor:
					if(stage==1)
					{
					SupervisorEvalApprovePanel seap = new SupervisorEvalApprovePanel(rid,client,myframe.getUserlogged());
					myframe.SetPanel(seap);break;
					} else
						if(stage == 3)
						{
							SupervisorEvaluationTimelimit sETLPanel = new SupervisorEvaluationTimelimit(rid,client);
							myframe.SetPanel(sETLPanel);
						}else
							if (stage == 10) {
								SupervisorPLTimelimitPanel sPLT = new SupervisorPLTimelimitPanel(rid,client);
								myframe.SetPanel(sPLT);
							}
					break;
					case Evaluator: 
					
					
					if(stage==2)  // Evaluator needs to set up his deadline
					{
						EvaluatorSetDeadLinePanel evdeadline= new EvaluatorSetDeadLinePanel(client,rid);
						myframe.SetPanel(evdeadline);
					}
					
					else
						if(stage==4) // evaluator need to put his report in the system
						{
						//	EvaluatorReportPanel evreppanel= new EvaluatorReportPanel(rid, client);
						     EvaluatorPanel evpanel=new EvaluatorPanel(rid, client);
							myframe.SetPanel(evpanel);
						}
				}
				
			}
		});
		add(btnSelect);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.setBounds(10, 8, 89, 23);
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				myframe.logout();
			}
		});
		add(btnLogOut);
		
		btnSuspend = new JButton("Suspend/Release");
		btnSuspend.setBounds(105, 465, 91, 23);
		btnSuspend.setVisible(false);
		btnSuspend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int rid = Integer.parseInt(ridCB.getSelectedItem().toString());
				if (controller.getSuspendState(rid))
					controller.release(rid);
					else
						controller.suspend(rid);
				myframe.SetMainUserPanel();
				
			}
		});
		add(btnSuspend);
		
		JButton btnAddChangeRequest = new JButton("Add change request");
		btnAddChangeRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				myframe.setInitiatorPanel();
			}
		});
		btnAddChangeRequest.setBounds(293, 8, 135, 23);
		add(btnAddChangeRequest);

	}
	public static void main(String[] args)

	{

		CollegeClientGUI aFrame = new CollegeClientGUI();

	}
}
